package com.chinaisafe.user.service.impl;

import com.chinaisafe.user.entity.SysRoleMenu;
import com.chinaisafe.user.mapper.SysRoleMenuMapper;
import com.chinaisafe.user.service.SysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysRoleMenuReqVO;
import com.chinaisafe.user.vo.response.SysRoleMenuRespVO;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysRoleMenuServiceImpl
 * Description：角色与菜单服务实现类
 *
 * @author xuefu.shu
 * @date 2024-07-13 17:14:06
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public IPage<SysRoleMenuRespVO> getPageList(SysRoleMenuReqVO sysRoleMenuReqVO) {
        // 分页设置
        Page<SysRoleMenu> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysRoleMenuReqVO);
        // 查询条件构造
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenu::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRoleMenuRespVO add(SysRoleMenuReqVO sysRoleMenuReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysRoleMenuReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRoleMenuRespVO update(SysRoleMenuReqVO sysRoleMenuReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysRoleMenuReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRoleMenuRespVO addOrUpdate(SysRoleMenuReqVO sysRoleMenuReqVO) {
        Long sysRoleMenuId = sysRoleMenuReqVO.getId();
        if (sysRoleMenuId != null){
            // 编辑
            SysRoleMenu sysRoleMenu = baseMapper.selectById(sysRoleMenuId);
            Optional.ofNullable(sysRoleMenu).orElseThrow(() -> BizException.build("未找到要修改的角色与菜单信息"));
        }
        SysRoleMenu sysRoleMenu2Save = sysRoleMenuReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysRoleMenu2Save);
        if (success) {
            return SysRoleMenuRespVO.convertFromEntity(sysRoleMenu2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysRoleMenuRespVO getDetailById(Long sysRoleMenuId) {
        SysRoleMenu sysRoleMenu = baseMapper.selectById(sysRoleMenuId);
        Optional.ofNullable(sysRoleMenu).orElseThrow(() -> BizException.build("未找到对应角色与菜单信息"));
        return SysRoleMenuRespVO.convertFromEntity(sysRoleMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysRoleMenuIds) {
        if (sysRoleMenuIds.length > 0) {
            this.removeByIds(Arrays.asList(sysRoleMenuIds));
        }
    }
}
